package com.bxd.baodan.util;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.db.DBHelper;
import com.bxd.baodan.item.CityItem;
import com.bxd.baodan.item.ProvinceItem;
import com.bxd.baodan.item.UserItem;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * 保存登录后用户信息的帮助类
 * @author Administrator
 *
 */
public class SaveMsgUtil {
	/**
	 * 保存用户登录后的返回信息到本地
	 * @param user
	 * @param context
	 */
	public static void updateLocalInfo(UserItem user, Context context) {
		if(user == null){
			return;//对应清除数据以后的情况
		}
		Dao<ProvinceItem, ?> dao;
		String proName = "";
		try {
			dao = DBHelper.getInstance(context).getDao(ProvinceItem.class);
			List<ProvinceItem> temp = dao.queryBuilder().where().eq("provinceCode", user.getUe_prncode()).query();
			if(temp!=null&&temp.size() > 0){
				proName = temp.get(0).getName();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Dao<CityItem, ?> daoCity;
		try {
			daoCity = DBHelper.getInstance(context).getDao(CityItem.class);
			List<CityItem> temp = daoCity.queryBuilder().where().eq("cityCode", user.getUe_citycode()).query();
			if(temp != null && temp.size() > 0){
				proName = proName + temp.get(0).getName();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		SpUtil.getInstance().savaString(SharedPreferanceKey.AREA, proName);
		String sex = "";
		if("0".equals(user.getUe_sex())){
			sex = "未知";
		}else{
			sex = "1".equals(user.getUe_sex())?"男":"女";
		}
		SpUtil.getInstance().savaString(SharedPreferanceKey.SEX, sex);
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); 
		Date date = null;    
		 try {
			date = format1.parse(user.getBirthday());
			SpUtil.getInstance().savaLong(SharedPreferanceKey.BIRTHDAY, date.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SpUtil.getInstance().savaString(SharedPreferanceKey.ACCOUNT, user.getPhone());
		SpUtil.getInstance().savaString(SharedPreferanceKey.WENWEN_TITLE, user.getTitle());
		SpUtil.getInstance().savaString(SharedPreferanceKey.TEMPICURL, user.getUe_avatar());
		SpUtil.getInstance().savaString(SharedPreferanceKey.NICK_NAME, user.getU_nick());
		SpUtil.getInstance().savaString(SharedPreferanceKey.QQ, user.getQq());
		SpUtil.getInstance().savaString(SharedPreferanceKey.AREA, user.getCity());
	}

}
